<?php

class Admin_Form_User extends Zend_Form {

    private $id; // l'id de l'utilisateur

    public function init() {
// le nom du formulaire
        $this->setName('formUser');

//element hidden id pour l'utilisation du formulaire lors de la modification d'un utilisateur
        $id = new Zend_Form_Element_Hidden('id');

//element texte firstname
        $firstname = new Zend_Form_Element_Text('firstname');
        $firstname->setLabel('form_user_firstname')
                ->setRequired(true)
                ->addValidator('notEmpty')
                ->addFilter('StripTags')
                ->addFilter('StringTrim');

//element texte lastname
        $lastname = new Zend_Form_Element_Text('lastname');
        $lastname->setLabel('form_user_lastname')
                ->setRequired(true)
                ->addValidator('notEmpty')
                ->addFilter('StripTags')
                ->addFilter('StringTrim');

//element texte email
        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('form_user_email')
                ->setRequired(true)
//  vérifie que l’adresse mail est correcte est qu'elle  n’est pas déjà dans la base de donnée 
//  le premier paramètre est le nom de la table, le second l’attribut qui doit être unique.
                ->addValidator('EmailAddress')
                ->addValidator(new Zend_Validate_Db_NoRecordExists('User', 'email'))
                ->addFilter('StripTags')
                ->addFilter('StringTrim');

//element texte password
        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('form_user_password')
                ->setRequired(true)
                ->addValidator('notEmpty')
                ->addFilter('StripTags')
                ->addFilter('StringTrim');

//element texte password2
        $password2 = new Zend_Form_Element_Password('password2');
        $password2->setLabel('form_user_password2')
                ->setRequired(true)
                ->addValidator('notEmpty')
                ->addFilter('StripTags')
                ->addFilter('StringTrim');

//bouton submit
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton')
                ->setLabel('form_user_submit');

// on ajoute tous les éléments du formulaire dans un tableau
        $elements = array($id, $firstname, $lastname, $email, $password, $password2, $submit);

// on ajoute ce tableau d'éléments au formulaire
        $this->addElements($elements);

        $id = $this->getId();
        if (isset($id) && $id != "") {
            $user = new Application_Model_DbTable_User ( );
            $userFound = $user->getById($id)->toArray();
            if ($userFound != null) {
                $this->populate($userFound[0]);
            } else {
                throw new Zend_Exception("form_user_no_id");
            }
            $password->setDescription("form_user_update_password_change");
            $password->setRequired(false);
            $submit->setLabel('form_user_update_submit');
        }
    }

    public function isValid($data) {
        $this->getElement('password')->addValidator(new App_Validate_PasswordMatch($data ['password2']));
        if ($this->getElement('email')->getValue() == $data ['email']) {
            $this->getElement('email')->removeValidator("Zend_Validate_Db_NoRecordExists");
        }
        return parent::isValid($data);
    }

    /**
     * @param $idUser the $idUser to set
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @return the $idUser
     */
    public function getId() {
        return $this->id;
    }

}

class App_Validate_PasswordMatch extends Zend_Validate_Abstract {

    const PASSWORD_MISMATCH = 'passwordMismatch';

    protected $_compare;
    protected $_messageTemplates = array(self::PASSWORD_MISMATCH => "PASSWORD_MISMATCH");

    public function __construct($compare) {
        $this->_compare = $compare;
    }

    public function isValid($value) {
        $this->_setValue((string) $value);
        if ($value !== $this->_compare) {
            $this->_error(self::PASSWORD_MISMATCH);
            return false;
        }
        return true;
    }

}

